// pages/product/index/index.js
const app = getApp();
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        list2: [],
        data: {},
    },
    onShow() {
        this.getTabBar().setData({
            selected: 2
        })
    },
    onLoad(options) {
       this.fetch()
    },
    fetch(){
        db.collection('case_info').get({
            success: res => {
                const data = res.data[0];
                this.setData({
                    data
                })
                db.collection('case').get({
                    success: res => {
                        this.setData({
                            list: res.data
                        })
                    },
                    fail: f => {
                        console.log('f', f)
                    }
                })
                db.collection('case2').get({
                    success: res => {
                        console.log(res)
                        this.setData({
                            list2: res.data
                        })
                        wx.stopPullDownRefresh()
                    },
                    fail: f => {
                        console.log('f', f)
                        wx.stopPullDownRefresh()
                    }
                })
            },
            fail: f => {
                console.log('f', f)
            }
        })
    },
    toDetail(e) {
        const index = e.currentTarget.dataset.index;
        const data = this.data.list[index];console.log(data)
        wx.setStorageSync('caseDetail', data);
        wx.navigateTo({
            url: `../detail/index?name=${data.name}`,
            fail: f => {
                console.log('f', f)
            }
        })
    },
    toDetail2(e) {
        const index = e.currentTarget.dataset.index;
        const data = this.data.list2[index];console.log(data)
        wx.setStorageSync('caseDetail', data);
        wx.navigateTo({
            url: `../detail/index?name=${data.name}`,
            fail: f => {
                console.log('f', f)
            }
        })
    },
    onPullDownRefresh(){
        this.fetch();
    }
})